package com.swz.dcrm.ui.home;


import android.animation.ValueAnimator;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bigkoo.pickerview.TimePickerView;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.google.gson.Gson;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.Approval;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Boutique;
import com.swz.dcrm.model.Car;
import com.swz.dcrm.model.CarDetail;
import com.swz.dcrm.model.CarModel;
import com.swz.dcrm.model.CarOrder;
import com.swz.dcrm.model.Clue;
import com.swz.dcrm.model.ClueDetail;
import com.swz.dcrm.model.ClueRecord;
import com.swz.dcrm.model.Insurance;
import com.swz.dcrm.model.Quotation;
import com.swz.dcrm.model.QuotationBoutique;
import com.swz.dcrm.model.QuotationInsurance;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.car.CarBrandFragmentArgs;
import com.swz.dcrm.ui.mine.EditInfoFragmentArgs;
import com.swz.dcrm.ui.viewmodel.CarBrandViewModel;
import com.swz.dcrm.ui.viewmodel.CarManagementViewModel;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.ui.viewmodel.TodayPlanViewModel;
import com.swz.dcrm.util.DateUtils;
import com.swz.dcrm.util.Tool;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class AddOrderFragment extends BaseFragment implements Validator.ValidationListener {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_right)
    TextView tvRight;

    @BindView(R.id.tv_name)
    @NotEmpty(messageResId = R.string.not_be_null_name)
    TextView tvName;
    @BindView(R.id.rg_sex)
    RadioGroup rgSex;

    @BindView(R.id.et_phone)
    @NotEmpty(messageResId = R.string.not_be_null_phone)
    TextView etPhone;
    @BindView(R.id.et_wechat)
    TextView etWechat;
    @BindView(R.id.ll_wechat)
    LinearLayout llWechat;
    @BindView(R.id.rg_wechat)
    RadioGroup rgWechat;

    @BindView(R.id.et_city)
    EditText etCity;

    @BindView(R.id.tv_car_num)
    TextView tvCarNum;
    @BindView(R.id.rg_replace)
    RadioGroup rgReplace;

    @BindView(R.id.tv_car_price)
    TextView tvCarPrice;
    @BindView(R.id.tv_acquisition_tax)
    TextView tvAcquisitionTax;
    @BindView(R.id.tv_boutique_fee)
    TextView tvBoutiqueFee;
    @BindView(R.id.tv_insurance_fee)
    TextView tvInsuranceFee;
    @BindView(R.id.tv_loan_fee)
    TextView tvLoanFee;
    @BindView(R.id.tv_maintenance_fee)
    TextView tvMaintenanceFee;
    @BindView(R.id.tv_other_fee)
    TextView tvOtherFee;
    @BindView(R.id.tv_license_plate_fee)
    TextView tvLicensePlateFee;
    @BindView(R.id.tv_total_fee)
    TextView tvTotalFee;
    @BindView(R.id.tv_brand)
    TextView tvBrand;

    @BindView(R.id.tv_car_model)
    @NotEmpty(messageResId = R.string.not_be_null_car_model)
    TextView tvCarModel;

    @BindView(R.id.tv_delivery_date)
    @NotEmpty(messageResId = R.string.not_be_null_delivery_date)
    TextView tvDeliveryDate;

    @BindView(R.id.tv_order_date)
    @NotEmpty(messageResId = R.string.not_be_null_order_date)
    TextView tvOrderDate;

    @BindView(R.id.tv_earnest)
    @NotEmpty(messageResId = R.string.not_be_null_earnest)
    TextView tvEarnest;

    @BindView(R.id.tv_contract_amount)
    @NotEmpty(messageResId = R.string.not_be_null_contract_amount)
    TextView tvContractAmount;

    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.tv_color)
    TextView tvColor;

    @BindView(R.id.tv_color_state)
    TextView tvColorState;
    private Clue clue;
    private Quotation quotation;
    private int height;
    private ValueAnimator valueAnimator;
    private TimePickerView timePickerView;
    private int currentChoice;
    private ActionSheetDialog colorDialog;
    private boolean clickable=true;

    @Inject
    MainViewModel mainViewModel;
    @Inject
    TodayPlanViewModel todayPlanViewModel;
    @Inject
    CarManagementViewModel carManagementViewModel;

    @Inject
    CarBrandViewModel carBrandViewModel;

    Validator validator;

    public static AddOrderFragment newInstance() {

        return new AddOrderFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())) {
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_add_order;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.add_order_title));
        tvRight.setText(getString(R.string.add_order_save));
        validator = new Validator(this);
        validator.setValidationListener(this);
        timePickerView = getTimePickerView((date, v) -> {
            if (currentChoice == 0) {
                tvDeliveryDate.setText(DateUtils.dateFormat(date, "yyyy-MM-dd"));
                clue.setPredictDeliveryCarTime(DateUtils.dateFormat(date, "yyyy-MM-dd"));
            } else {
                tvOrderDate.setText(DateUtils.dateFormat(date, "yyyy-MM-dd"));
                clue.setOrderTime(DateUtils.dateFormat(date, "yyyy-MM-dd"));
            }
        }).build();
        height = Tool.dip2px(getActivity(), 54);
        rgWechat.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_no:
                    clue.setWeixinFriend(false);
                    valueAnimator = ValueAnimator.ofInt(height, 0);
                    valueAnimator.addUpdateListener(animation -> {
                        ViewGroup.LayoutParams layoutParams = llWechat.getLayoutParams();
                        layoutParams.height = (int) animation.getAnimatedValue();
                        llWechat.setLayoutParams(layoutParams);
                    });
                    valueAnimator.setDuration(100);
                    valueAnimator.start();
                    break;
                case R.id.rb_yes:
                    clue.setWeixinFriend(true);
                    valueAnimator = ValueAnimator.ofInt(0, height);
                    valueAnimator.addUpdateListener(animation -> {
                        ViewGroup.LayoutParams layoutParams = llWechat.getLayoutParams();
                        layoutParams.height = (int) animation.getAnimatedValue();
                        llWechat.setLayoutParams(layoutParams);
                    });
                    valueAnimator.setDuration(100);
                    valueAnimator.start();
                    break;
            }
        });
        rgSex.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_boy:
                    clue.setSex(1);
                    break;
                case R.id.rb_girl:
                    clue.setSex(0);
                    break;
            }
        });
        rgReplace.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_replace_yes:
                    clue.setReplacementCar(true);
                    break;
                case R.id.rb_replace_no:
                    clue.setReplacementCar(false);
                    break;
            }
        });
        AddOrderFragmentArgs addOrderFragmentArgs = AddOrderFragmentArgs.fromBundle(getArguments());

        if(!addOrderFragmentArgs.getOrderId().equals("test")){
            carManagementViewModel.getCarOrderDetail(Long.valueOf(addOrderFragmentArgs.getOrderId()))
                    .observe(this, new Observer<BaseResponse<CarOrder>>() {
                        @Override
                        public void onChanged(BaseResponse<CarOrder> carOrderBaseResponse) {
                            if(carOrderBaseResponse==null){
                                return;
                            }
                            if(carOrderBaseResponse.isSuccess()){
                                CarOrder carOrder=carOrderBaseResponse.getData();
                                clue=carOrder.getClueVO();
                                initClue(carOrder.getClueVO());
                                quotation=carOrder.getQuotationVO();
                                initQuotation(carOrder.getQuotationVO());
                                tvColor.setText(carOrder.getCarColor());
                                tvCarNum.setText(carOrder.getCarFrame());
                                tvOrderDate.setText(DateUtils.dateFormat(carOrder.getOrderTime(),"yyyy-MM-dd"));
                                etRemark.setText(carOrder.getOrderRemark());
                                tvContractAmount.setText(carOrder.getContractAmount()+"");
                                tvEarnest.setText(carOrder.getDepositAmount()+"");
                                tvDeliveryDate.setText(DateUtils.dateFormat(clue.getPredictDeliveryCarTime(),"yyyy-MM-dd"));
                                tvRight.setText("");
                                title.setText(getString(R.string.add_order_title1));
                                clickable=false;
                            }
                        }
                    });
        }
        if (!addOrderFragmentArgs.getApproval().equals("test")) {
            Approval approval = new Gson().fromJson(addOrderFragmentArgs.getApproval(), Approval.class);
            carManagementViewModel.getQuotationById(approval.getBusinessId())
                    .observe(this, quotationBaseResponse -> {
                        quotation = quotationBaseResponse.getData();
                        initQuotation(quotation);
                    });
            todayPlanViewModel.getClueDetail(approval.getClueId())
                    .observe(this, clueDetailBaseResponse -> {
                        if (clueDetailBaseResponse == null) {
                            return;
                        }
                        clue = clueDetailBaseResponse.getData().getBsClueVO();
                        initClue(clue);
                    });

        } else {
            if (!addOrderFragmentArgs.getClue().equals("test")) {
                clue = new Gson().fromJson(addOrderFragmentArgs.getClue(), Clue.class);
                initClue(clue);
            } else {
                clue = new Clue();
            }
            if (!addOrderFragmentArgs.getQuotation().equals("test")) {
                quotation = new Gson().fromJson(addOrderFragmentArgs.getQuotation(), Quotation.class);
                initQuotation(quotation);
            } else {
                quotation = new Quotation();
            }
            carManagementViewModel.getCurrentSelectedClueRecord().observe(this, clue -> {
                if(clue!=null){
                    this.clue=clue;
                    initClue(clue);
                }
            });
        }

        carBrandViewModel.getSelectedCarSeries().observe(this, carSeries -> {
            if (carSeries != null) {
                quotation.setCarSeriesId(carSeries.getId());
                quotation.setCarSeriesName(carSeries.getName());
                tvBrand.setText(carBrandViewModel.getSelectedCarBrand().getValue().getName() + "-" +
                        carSeries.getName());
            }
        });
        carBrandViewModel.getSelectedCarModel().observe(this, carModel -> {
            if (carModel != null) {
                quotation.setCarModelId(carModel.getId());
                quotation.setCarModelName(carModel.getName());
                tvCarModel.setText(carModel.getName());
                tvCarNum.setText("");
                tvColor.setText("");


                carManagementViewModel.getCarDetailParam(carModel.getId())
                        .observe(this, new Observer<BaseResponse<CarDetail>>() {
                            @Override
                            public void onChanged(BaseResponse<CarDetail> carDetailBaseResponse) {
                                carManagementViewModel.getCarDetailParam().removeObserver(this);
                                if (carDetailBaseResponse.isSuccess()) {
                                    tvColorState.setText("");

                                    String[] color = carDetailBaseResponse.getData().getColor().split(";");
                                    colorDialog = new ActionSheetDialog(getContext(), color, null);
                                    colorDialog.isTitleShow(false);
                                    colorDialog.setOnOperItemClickL((parent, view, position, id) -> {
                                        tvColor.setText(color[position]);

                                        carManagementViewModel.getCarInWareHouse(color[position], carDetailBaseResponse.getData().getId())
                                                .observe(AddOrderFragment.this, new Observer<BaseResponse<Car>>() {
                                                    @Override
                                                    public void onChanged(BaseResponse<Car> carBaseResponse) {
                                                        if (carBaseResponse.getData() == null) {
                                                            tvCarNum.setText("");
                                                            tvColorState.setText(getString(R.string.add_order_no_car));
                                                        } else {
                                                            tvCarNum.setText(carBaseResponse.getData().getCarFrame());
                                                            tvColorState.setText("");

                                                        }
                                                    }
                                                });
                                        colorDialog.dismiss();
                                    });
                                }
                            }
                        });

                carManagementViewModel.getCarModelDetail(carModel.getId())
                        .observe(this, new Observer<BaseResponse<CarModel>>() {
                            @Override
                            public void onChanged(BaseResponse<CarModel> carModelBaseResponse) {
                                if(carModelBaseResponse==null){
                                    return;
                                }
                                carManagementViewModel.getCarModelDetail().removeObserver(this);
                                if (carModelBaseResponse.isSuccess()) {
                                    CarModel mCarModel = carModelBaseResponse.getData();
                                    tvCarPrice.setText(Tool.formatToNumber(new BigDecimal(mCarModel.getCarPrice())));
                                    tvAcquisitionTax.setText(Tool.formatToNumber(new BigDecimal(mCarModel.getAcquisitionTax())));
                                    tvLoanFee.setText(Tool.formatToNumber(new BigDecimal(mCarModel.getLoanFee())));
                                    tvLicensePlateFee.setText(Tool.formatToNumber(new BigDecimal(mCarModel.getLicensePlateFee())));
                                    tvMaintenanceFee.setText(Tool.formatToNumber(new BigDecimal(mCarModel.getMaintenanceFee())));
                                    tvOtherFee.setText(Tool.formatToNumber(new BigDecimal(mCarModel.getOtherFee())));
                                    //todo 报价单
                                    quotation.setAcquisitionTax(mCarModel.getAcquisitionTax());
                                    quotation.setCarPrice(mCarModel.getCarPrice());
                                    quotation.setLicensePlateFee(mCarModel.getLicensePlateFee());
                                    quotation.setOtherFee(mCarModel.getOtherFee());
                                    quotation.setLoanFee(mCarModel.getLoanFee());
                                    quotation.setMadoubleenanceFee(mCarModel.getMaintenanceFee());
                                    BigDecimal bigDecimal = new BigDecimal(mCarModel.getCarPrice());
                                    bigDecimal = bigDecimal.add(new BigDecimal(mCarModel.getAcquisitionTax()));
                                    bigDecimal = bigDecimal.add(new BigDecimal(mCarModel.getLoanFee()));
                                    bigDecimal = bigDecimal.add(new BigDecimal(mCarModel.getLicensePlateFee()));
                                    bigDecimal = bigDecimal.add(new BigDecimal(mCarModel.getMaintenanceFee()));
                                    bigDecimal = bigDecimal.add(new BigDecimal(tvBoutiqueFee.getText().toString()));
                                    bigDecimal = bigDecimal.add(new BigDecimal(tvInsuranceFee.getText().toString()));
                                    tvTotalFee.setText(bigDecimal.doubleValue() + "");
                                }

                            }
                        });
            }
        });
        mainViewModel.getEdit().observe(this, edit -> {
            if (edit == null || TextUtils.isEmpty(edit.getValue())) {
                return;
            }
            if (edit.getTitle().equals(getString(R.string.feel_in_total_price_tag1))) {
                tvCarPrice.setText(edit.getValue());
                quotation.setCarPrice(Double.valueOf(edit.getValue()));
            } else if (edit.getTitle().equals(getString(R.string.feel_in_total_price_tag2))) {
                tvAcquisitionTax.setText(edit.getValue());
                quotation.setAcquisitionTax(Double.valueOf(edit.getValue()));

            } else if (edit.getTitle().equals(getString(R.string.feel_in_total_price_tag3))) {
                tvLicensePlateFee.setText(edit.getValue());
                quotation.setLicensePlateFee(Double.valueOf(edit.getValue()));

            } else if (edit.getTitle().equals(getString(R.string.feel_in_total_price_tag5))) {
                tvLoanFee.setText(edit.getValue());
                quotation.setLoanFee(Double.valueOf(edit.getValue()));

            } else if (edit.getTitle().equals(getString(R.string.feel_in_total_price_tag7))) {
                tvMaintenanceFee.setText(edit.getValue());
                quotation.setMaintenanceFee(Double.valueOf(edit.getValue()));

            } else if (edit.getTitle().equals(getString(R.string.feel_in_total_price_tag8))) {
                tvOtherFee.setText(edit.getValue());
                quotation.setOtherFee(Double.valueOf(edit.getValue()));

            } else if (edit.getTitle().equals(getString(R.string.feel_in_total_price_tag11))) {
                tvEarnest.setText(edit.getValue());
            } else if (edit.getTitle().equals(getString(R.string.feel_in_total_price_tag10))) {
                tvContractAmount.setText(edit.getValue());
            }
            BigDecimal bigDecimal = new BigDecimal(tvCarPrice.getText().toString());
            bigDecimal = bigDecimal.add(new BigDecimal(tvAcquisitionTax.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvLicensePlateFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvLoanFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvMaintenanceFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvOtherFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvBoutiqueFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvInsuranceFee.getText().toString()));
            tvTotalFee.setText(bigDecimal.toString());
        });
        carManagementViewModel.getSelectedInsurance().observe(this, insurances -> {
            BigDecimal bigDecimal = new BigDecimal(tvCarPrice.getText().toString());
            bigDecimal = bigDecimal.add(new BigDecimal(tvAcquisitionTax.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvLicensePlateFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvLoanFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvMaintenanceFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvOtherFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvBoutiqueFee.getText().toString()));
            List<QuotationInsurance> list;
            if (insurances != null && insurances.size() > 0) {
                list = new ArrayList<>();
                BigDecimal boutiquePrices = null;
                for (Insurance insurance : insurances) {
                    QuotationInsurance quotationInsurance = new QuotationInsurance();
                    quotationInsurance.setCarModelId(insurance.getCarModelId());
                    quotationInsurance.setPrice(Double.valueOf(insurance.getPrice()));
                    quotationInsurance.setInsuranceConfigId(insurance.getId());
                    list.add(quotationInsurance);
                    if (insurance.getPrice() == null) {
                        continue;
                    }
                    if (boutiquePrices == null) {
                        boutiquePrices = new BigDecimal(insurance.getPrice());
                    } else {
                        boutiquePrices = boutiquePrices.add(new BigDecimal(insurance.getPrice()));
                    }
                    bigDecimal = bigDecimal.add(new BigDecimal(insurance.getPrice()));
                }
                quotation.setQuotationInsuranceList(list);
                quotation.setInsurancePrice(boutiquePrices.doubleValue());
                tvInsuranceFee.setText(boutiquePrices.toString());
            } else {
                quotation.setQuotationInsuranceList(null);
                quotation.setInsurancePrice(0);
                tvInsuranceFee.setText("0");
            }


            quotation.setNormalCarFullPrice(bigDecimal.doubleValue());
            tvTotalFee.setText(bigDecimal.toString());
        });
        carManagementViewModel.getSelectedBoutique().observe(this, boutiques -> {
            BigDecimal bigDecimal = new BigDecimal(tvCarPrice.getText().toString());
            bigDecimal = bigDecimal.add(new BigDecimal(tvAcquisitionTax.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvLicensePlateFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvLoanFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvMaintenanceFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvOtherFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvInsuranceFee.getText().toString()));
            if (boutiques != null && boutiques.size() > 0) {
                List<QuotationBoutique> list = new ArrayList<>();
                BigDecimal boutiquePrices = null;
                for (Boutique boutique : boutiques) {
                    QuotationBoutique quotationBoutique = new QuotationBoutique();
                    quotationBoutique.setBoutiqueId(boutique.getId());
                    quotationBoutique.setBoutiqueName(boutique.getBoutiqueName());
                    quotationBoutique.setPrice(Double.valueOf(boutique.getPrice()));
                    list.add(quotationBoutique);
                    if (boutique.getPrice() == null) {
                        continue;
                    }
                    if (boutiquePrices == null) {
                        boutiquePrices = new BigDecimal(boutique.getPrice());
                    } else {
                        boutiquePrices = boutiquePrices.add(new BigDecimal(boutique.getPrice()));
                    }
                    bigDecimal = bigDecimal.add(new BigDecimal(boutique.getPrice()));
                }
                quotation.setQuotationBoutiqueList(list);
                quotation.setBoutiquePrice(boutiquePrices.doubleValue());
                tvBoutiqueFee.setText(boutiquePrices.toString());
            } else {
                quotation.setQuotationBoutiqueList(null);
                quotation.setBoutiquePrice(0);
                tvBoutiqueFee.setText("0");
            }
            quotation.setNormalCarFullPrice(bigDecimal.doubleValue());
            tvTotalFee.setText(bigDecimal.toString());
        });

        return true;
    }
    private void initQuotation(Quotation quotation){
        tvBoutiqueFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getBoutiquePrice())));
        tvInsuranceFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getInsurancePrice())));
        tvLoanFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getLoanFee())));
        tvAcquisitionTax.setText(Tool.formatToNumber(new BigDecimal(quotation.getAcquisitionTax())));
        tvMaintenanceFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getMadoubleenanceFee())));
        tvOtherFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getOtherFee())));
        tvCarPrice.setText(Tool.formatToNumber(new BigDecimal(quotation.getCarPrice())));
        tvTotalFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getNormalCarFullPrice())));
        tvLicensePlateFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getLicensePlateFee())));
        tvCarModel.setText(quotation.getCarModelName());
        tvBrand.setText(quotation.getCarSeriesName());
    }
    private void initClue(Clue clue){
        if (!TextUtils.isEmpty(clue.getCusName())) {
            tvName.setText(clue.getCusName());
        }
        if (!TextUtils.isEmpty(clue.getCusPhone())) {
            etPhone.setText(clue.getCusPhone());
        }
        if(clue.getSex()==null){
            rgSex.check(R.id.rb_unkown);
        }else {
            if (clue.isBoy()) {
                rgSex.check(R.id.rb_boy);
            } else {
                rgSex.check(R.id.rb_girl);
            }
        }

        if (clue.getWeixinFriend()) {
            etWechat.setText(clue.getWechat());
            rgWechat.check(R.id.rb_yes);

        } else {
            rgWechat.check(R.id.rb_no);
        }
        if (!TextUtils.isEmpty(clue.getLicensePlateCity())) {
            etCity.setText(clue.getLicensePlateCity());
        }
    }
    @OnClick({R.id.ll_car_price, R.id.ll_license_plate_fee, R.id.ll_maintenance_fee, R.id.ll_other_fee, R.id.ll_loan_fee, R.id.ll_acquisition_tax,
            R.id.ll_earnest, R.id.ll_contract_amount})
    public void edit(View view) {
        if(!clickable){
            return;
        }
        EditInfoFragmentArgs.Builder builder = new EditInfoFragmentArgs.Builder();
        builder.setInputType(String.valueOf(8194));
        switch (view.getId()) {
            case R.id.ll_car_price:
                builder.setTitle(getString(R.string.feel_in_total_price_tag1));
                builder.setValue(tvCarPrice.getText().toString());
                break;
            case R.id.ll_acquisition_tax:
                builder.setTitle(getString(R.string.feel_in_total_price_tag2));
                builder.setValue(tvAcquisitionTax.getText().toString());
                break;
            case R.id.ll_license_plate_fee:
                builder.setTitle(getString(R.string.feel_in_total_price_tag3));
                builder.setValue(tvLicensePlateFee.getText().toString());
                break;
            case R.id.ll_loan_fee:
                builder.setTitle(getString(R.string.feel_in_total_price_tag5));
                builder.setValue(tvLoanFee.getText().toString());
                break;
            case R.id.ll_maintenance_fee:
                builder.setTitle(getString(R.string.feel_in_total_price_tag7));
                builder.setValue(tvMaintenanceFee.getText().toString());
                break;
            case R.id.ll_other_fee:
                builder.setTitle(getString(R.string.feel_in_total_price_tag8));
                builder.setValue(tvOtherFee.getText().toString());
                break;
            case R.id.ll_earnest:
                builder.setTitle(getString(R.string.feel_in_total_price_tag11));
                builder.setValue(tvEarnest.getText().toString());
                break;
            case R.id.ll_contract_amount:
                builder.setTitle(getString(R.string.feel_in_total_price_tag10));
                builder.setValue(tvContractAmount.getText().toString());
                break;
        }
        go(null, R.id.action_addOrderFragment_to_editInfoFragment, builder.build().toBundle());
    }

    @OnClick({R.id.ll_brand, R.id.ll_model, R.id.ll_boutique, R.id.ll_insurance_fee})
    public void selectCarBrand(View view) {
        if(!clickable){
            return;
        }
        switch (view.getId()) {
            case R.id.ll_brand:
                go(null, R.id.action_addOrderFragment_to_carBrandFragment, null);
                break;
            case R.id.ll_model:
                if (carBrandViewModel.getSelectedCarBrand().getValue() != null) {
                    CarBrandFragmentArgs.Builder builder = new CarBrandFragmentArgs.Builder();
                    builder.setBrand(new Gson().toJson(carBrandViewModel.getSelectedCarBrand().getValue()));
                    go(null, R.id.action_addOrderFragment_to_carBrandFragment, builder.build().toBundle());
                } else {
                    showToast("请先选择车型");
                }
                break;
            case R.id.ll_boutique:
                go(null, R.id.action_addOrderFragment_to_boutiqueFragment, null);
                break;
            case R.id.ll_insurance_fee:
                go(null, R.id.action_addOrderFragment_to_insuranceFragment, null);
                break;

        }

    }

    @OnClick({R.id.ll_delivery_date, R.id.ll_order_date})
    public void selectedDate(View view) {
        if(!clickable){
            return;
        }
        switch (view.getId()) {
            case R.id.ll_delivery_date:
                currentChoice = 0;
                break;
            case R.id.ll_order_date:
                currentChoice = 1;
                break;

        }
        timePickerView.show();
    }

    @Override
    public void onDestroyView() {
        mainViewModel.getEdit().setValue(null);
        super.onDestroyView();
    }

    @OnClick(R.id.tv_right)
    public void save() {

        validator.validate();
    }

    @OnClick(R.id.ll_color)
    public void selectColor() {
        if(!clickable){
            return;
        }
        if (colorDialog == null) {
            showToast("请选择车型");
            return;
        }
        colorDialog.show();
    }

    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick(R.id.ll_name)
    public void selectClue(){
        if(!clickable){
            return;
        }
        go(null,R.id.action_addOrderFragment_to_clueSelectFragment,null);
    }

    @Override
    public void onValidationSucceeded() {
        CarOrder carOrder = new CarOrder();
        if (quotation.getId() != null) {
            carOrder.setQuotationId(quotation.getId());
        }
        if (clue.getId() != null) {
            carOrder.setClueId(clue.getId());
        }

        carOrder.setQuotationVO(quotation);
        carOrder.setClueVO(clue);
        carOrder.setPredictDeliveryCarTime(tvDeliveryDate.getText().toString());
        carOrder.setCarColor(tvColor.getText().toString());
        carOrder.setCarFrame(tvCarNum.getText().toString().trim());
        carOrder.setOrderRemark(etRemark.getText().toString());
        carOrder.setDepositAmount(Double.valueOf(tvEarnest.getText().toString().trim()));
        carOrder.setContractAmount(Double.valueOf(tvContractAmount.getText().toString().trim()));
        carManagementViewModel.addCarOrder(carOrder).observe(this, new Observer<BaseResponse<CarOrder>>() {
            @Override
            public void onChanged(BaseResponse<CarOrder> carOrderBaseResponse) {
                if(carOrderBaseResponse==null){
                    return;
                }
                showToast(carOrderBaseResponse.getMsg());
                if (carOrderBaseResponse.isSuccess()) {
                    backClick();
                }
                carManagementViewModel.getAddCarOrder().removeObserver(this);
            }
        });
    }

    @Override
    public void onValidationFailed(List<ValidationError> errors) {
        if (errors != null && errors.size() > 0) {
            String message = errors.get(0).getCollatedErrorMessage(getContext());
            View view = errors.get(0).getView();
            if (view instanceof EditText) {
                view.requestFocus();
            }
            showToast(message);
        }

    }
}
